#include "splitter.hpp"

Splitter::Splitter(Generator<float> &in, int count):
    in_(in)
{
    for (int i = 0; i < count; ++i)
        out_.push_back(Out(this, i));
}

Generator<float> &Splitter::operator[](int index)
{
    return out_[index];
}

float Splitter::operator()(int channel)
{
    if (channels_.empty() || channels_.size() == out_.size() || 
        channels_.find(channel) != channels_.end())
    {
        value_ = in_();
        channels_.clear();
    }
    channels_.insert(channel);
    return value_;
}

Splitter::Out::Out(Splitter *parent, int channel):
    parent_(parent),
    channel_(channel)
{}

float Splitter::Out::operator()()
{
    return (*parent_)(channel_);
}
